package com.hnkywl.sts.service.report;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.hnkywl.sts.dao.report.CwkcDao;
import com.hnkywl.sts.dao.report.PjkcDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 13-6-15
 * Time: 下午9:23
 * To change this template use File | Settings | File Templates.
 */
@Service
public class PjkcService {
    private PjkcDao pjkcDao;

    /**
     * 财务库存报表
     *
     * @param gsid
     * @param year
     * @param month
     * @return
     */
    public String pjkcList(Long gsid, Integer year, Integer month, Integer pjsflx) {
        Map<String, Object> result;
        if (pjsflx == 0) {//应收
            result = pjkcDao.pjkcysList(gsid, year, month);
        } else {//应付
            result = pjkcDao.pjkcyfList(gsid, year, month);
        }
        List<Map> rows = (List<Map>) result.get("rows");
        List<Map> footer = (List<Map>) result.get("footer");
        Gson gson = new GsonBuilder().create();
        String footerJson = gson.toJson(footer);
        String rowsJson = gson.toJson(rows);
        String json = "{\"rows\":" + rowsJson + ",\"footer\":" + footerJson + "}";
        return json;
    }

    public String pjkcDetailList(Long gsid, Integer sfType, Integer type, Integer status, Date startDate, Date endDate, Integer pageNo, Integer pageSize) {
        Calendar c = Calendar.getInstance();
        if(status!=0){
            c.setTime(endDate);
        }
        c.add(Calendar.DATE, 1);
        endDate = c.getTime();
        Map<String, Object> map = pjkcDao.pjkcDetail(gsid, sfType, type, status, startDate, endDate, pageNo, pageSize);
        Integer total = (Integer) map.get("total");
        Double hjje = (Double) map.get("hjje");
        List rows = (ArrayList) map.get("rows");
        return "{\"total\":" + total + ",\"rows\":" + new Gson().toJson(rows) + ",\"footer\":[{\"gsmc\":\"合计\",\"dqje\":" + hjje + "}]}";
    }

    public Map<String, Object> pjkcDetailListExport(Long gsid, Integer sfType, Integer type, Integer status, Date startDate, Date endDate) {
        if(endDate!=null){
            Calendar c = Calendar.getInstance();
            c.setTime(endDate);
            c.add(Calendar.DATE, 1);
            endDate = c.getTime();
        }
        Map<String, Object> map = pjkcDao.pjkcDetail(gsid, sfType, type, status, startDate, endDate, 0, 0);
        return map;
    }

    @Autowired
    public void setPjkcDao(PjkcDao pjkcDao) {
        this.pjkcDao = pjkcDao;
    }
}
